/*******************************************************************************
 * Copyright (c) 2008, 2009 Fabio Mauro - Alberto Terraneo.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.2
 * or any later version published by the Free Software Foundation.
 *     
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied 
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 * See the GNU Free Documentation License for more details.
 *     
 * You should have received a copy of the GNU Free Documentation 
 * License along with this distribution; if not, send a email to authors:
 * aterraneo <at> users.sourceforge.net
 * bigmoby09 <at> users.sourceforge.net
 *******************************************************************************/
package it.blueocean.acanto.aop.error;

import it.blueocean.acanto.web.AcantoPlatformApplication;
import it.blueocean.acanto.web.platform.components.errorPages.ExceptionCatcherPage;

import java.lang.reflect.Method;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.Application;
import org.apache.wicket.RestartResponseAtInterceptPageException;
import org.apache.wicket.WicketRuntimeException;
import org.springframework.aop.ThrowsAdvice;

public class ExceptionInterceptor implements ThrowsAdvice
{
	private static Log log = null;

	public ExceptionInterceptor()
	{
	}


	public void afterThrowing(Method m, Object[] args, Object target, Throwable ex)
	{
		log = LogFactory.getLog(target.getClass());

		log.error("Exception in method: " + m.getName() + " Exception is: " + ex.getMessage());
		
		Application app = Application.get();
        if (app instanceof AcantoPlatformApplication)
            ((AcantoPlatformApplication) app).getLogoffContext();
        else
            throw new WicketRuntimeException("Application is not a subclass of " + AcantoPlatformApplication.class);

		throw new RestartResponseAtInterceptPageException(ExceptionCatcherPage.class);  
	}

}
